﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Se7en.Admin.Common;
using Se7en.Admin.Model;
using Se7en.Admin.Business;
using ERMSys.Framework.Utilities;
using System.Data.Services.Client;

namespace Se7en.Admin.Web.Pages
{
    public partial class ManageUser : AdminPageBase
    {
        #region Properties

        public int SelectedUserId
        {
            get
            {
                return (int)(ViewState["SelectedUserId"]);
            }
            set
            {
                ViewState["SelectedUserId"] = value;
            }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindUsers();
        }

        protected void grdMangeUser_ActionClick(object sender, ERMSys.Web.Controls.GridViewActionClickArgs e)
        {
            int userID = e.Key.Value.ConvertToInt32();
            switch (e.ActionName)
            {
                case "Edit":
                    modalAccount.Show();
                    ctlUserEdit.LoadUserInfo(userID);
                    break;
                case "UpdateRole":
                    SelectedUserId = userID;
                    modalUpdateRole.Show();
                    ctlRoleList.BindRoles();
                    break;
            }

        }

        protected void grdMangeUser_Refresh(object sender, EventArgs e)
        {
            BindUsers();
        }

        protected void grdMangeUser_PageIndexChanged(object sender, EventArgs e)
        {
            BindUsers();
        }

        protected void grdMangeUser_AddNew(object sender, EventArgs e)
        {
            modalAccount.Show();
            ctlUserEdit.ClearInputData();
        }

        protected void ctlUserEdit_Success(object sender, EventArgs e)
        {
            modalAccount.Hide();
            BindUsers();
        }

        #region Update Role

        protected void btnAssignAllRole_Click(object sender, EventArgs e)
        {
            DBContext.Role_AssignAllRoleToUserId(this.SelectedUserId);
            DBContext.SaveAllChanges();
            modalUpdateRole.Hide();
            BindUsers();
        }

        protected void btnRemoveAllRole_Click(object sender, EventArgs e)
        {
            DBContext.Role_RemoveAllRoleOfUserId(this.SelectedUserId);
            DBContext.SaveAllChanges();
            modalUpdateRole.Hide();
            BindUsers();
        }

        protected void btnAssignRole_Click(object sender, EventArgs e)
        {
            DBContext.Role_AssignToUserId(ctlRoleList.SelectedRoleIds, this.SelectedUserId);
            DBContext.SaveAllChanges();
            modalUpdateRole.Hide();
            BindUsers();
        }

        protected void btnRemoveRole_Click(object sender, EventArgs e)
        {
            DBContext.Role_RemoveRoleOfUserId(ctlRoleList.SelectedRoleIds, this.SelectedUserId);
            DBContext.SaveAllChanges();
            modalUpdateRole.Hide();
            BindUsers();
        }

        #endregion //Update Role

        #endregion

        #region Methods

        private void BindUsers()
        {
            int total;
            List<User> data = DBContext.User_GetPaging(grdMangeUser.PageIndex, grdMangeUser.PageSize, out total);
            grdMangeUser.DataBind(data, total);
        }

        #endregion
    }
}
